package com.hpch.business.controller;

import cn.hutool.core.util.StrUtil;
import com.hpch.business.entity.Honor;
import com.hpch.business.param.HonorParam;
import com.hpch.business.service.HonorService;
import com.hpch.business.util.RoleUtil;
import com.hpch.common.core.annotation.OperationLog;
import com.hpch.common.core.web.*;
import com.hpch.common.system.entity.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 荣誉资质控制器
 *
 * @author Lly
 * @since 2025-03-06 11:39:01
 */
@Api(tags = "荣誉资质管理")
@RestController
@RequestMapping("/api/business/honor")
public class HonorController extends BaseController {
    @Resource
    private HonorService honorService;

    @PreAuthorize("hasAuthority('business:honor:list')")
    @OperationLog
    @ApiOperation("分页查询荣誉资质")
    @GetMapping("/page")
    public ApiResult<PageResult<Honor>> page(HonorParam param) {
        PageParam<Honor, HonorParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
//        return success(honorService.page(page, page.getWrapper()));
        // 使用关联查询
        if (RoleUtil.isCompany(getLoginUser().getRoles())) {
            param.setUserId(getLoginUserId());
        }
        return success(honorService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('business:honor:list')")
    @OperationLog
    @ApiOperation("查询全部荣誉资质")
    @GetMapping()
    public ApiResult<List<Honor>> list(HonorParam param) {
        PageParam<Honor, HonorParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
//        return success(honorService.list(page.getOrderWrapper()));
        // 使用关联查询
        if (RoleUtil.isCompany(getLoginUser().getRoles())) {
            param.setUserId(getLoginUserId());
        }
        return success(honorService.listRel(param));
    }

    @PreAuthorize("hasAuthority('business:honor:list')")
    @OperationLog
    @ApiOperation("根据id查询荣誉资质")
    @GetMapping("/{id}")
    public ApiResult<Honor> get(@PathVariable("id") Integer id) {
//        return success(honorService.getById(id));
        // 使用关联查询
        return success(honorService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('business:honor:save')")
    @OperationLog
    @ApiOperation("添加荣誉资质")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Honor honor) {
        if (StrUtil.hasBlank(honor.getTitle(), honor.getThumb())) {
            return fail("标题或荣誉资质图不能为空");
        }
        List<Role> roles = getLoginUser().getRoles();
        if (RoleUtil.isDevAdmin(roles) || RoleUtil.isSurAdmin(roles)) {
            if (honor.getUserId() == null) {
                return fail("请选择所属企业");
            }
        }
        if (honorService.edit(getLoginUser(), honor)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('business:honor:update')")
    @OperationLog
    @ApiOperation("修改荣誉资质")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Honor honor) {
        if (honor.getId() == null) {
            return fail("请选择所属企业");
        }
        if (StrUtil.hasBlank(honor.getTitle(), honor.getThumb())) {
            return fail("标题或荣誉资质图不能为空");
        }
        List<Role> roles = getLoginUser().getRoles();
        if (RoleUtil.isDevAdmin(roles) || RoleUtil.isSurAdmin(roles)) {
            if (honor.getUserId() == null) {
                return fail("请选择所属企业");
            }
        }
        if (honorService.edit(getLoginUser(), honor)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    /**
     * 修改状态
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('business:product:update')")
    @OperationLog
    @ApiOperation("修改企业产品表状态")
    @GetMapping("/status")
    public ApiResult<?> updateStatus(Integer id) {
        if (id == null) {
            return fail("id不能为空");
        }
        if (honorService.updateStatus(id)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @PreAuthorize("hasAuthority('business:honor:remove')")
    @OperationLog
    @ApiOperation("删除荣誉资质")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (honorService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('business:honor:save')")
    @OperationLog
    @ApiOperation("批量添加荣誉资质")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Honor> list) {
        if (honorService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('business:honor:update')")
    @OperationLog
    @ApiOperation("批量修改荣誉资质")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Honor> batchParam) {
        if (batchParam.update(honorService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('business:honor:remove')")
    @OperationLog
    @ApiOperation("批量删除荣誉资质")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (honorService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
